<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="属性标准值ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="id">
              <a-input v-model="model.id" placeholder="请输入属性标准值ID"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="槽号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slotName">
              <a-input v-model="model.slotName" placeholder="请输入槽号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="属性名称" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="propName">
              <a-input-search
                placeholder="选择属性"
                readOnly
                v-model="model.propName" 
                @search="doSelectDataPropertyList"
              />  
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标准值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standardValue">
              <a-input v-model="model.standardValue" placeholder="请输入标准值"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="最大值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maxValue">
              <a-input v-model="model.maxValue" placeholder="请输入最大值"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="最小值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="minValue">
              <a-input v-model="model.minValue" placeholder="请输入最小值"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <select-data-property-list-modal ref="selectDataPropertyList" @ok="selectDataPropertyOk" />
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import SelectDataPropertyListModal from '@/views/corp/dataProperty/SelectDataPropertyList'

export default {
  name: 'DataStandardForm',
  components: {
    SelectDataPropertyListModal,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: { 
        propNo:'',
        propName:''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        id: [{ required: true, message: '请输入属性标准值ID!' }],
        slotName: [{ required: true, message: '请输入槽号!' }],
        propName: [{ required: true, message: '请选择属性名称!' }],
        standardValue: [{ required: true, message: '请输入标准值!' }],
      },
      url: {
        add: '/dataStandard/dataStandard/add',
        edit: '/dataStandard/dataStandard/edit',
        queryById: '/dataStandard/dataStandard/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    doSelectDataPropertyList() {
      this.$refs.selectDataPropertyList.show(this.propType)
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }, 
    selectDataPropertyOk(obj) {
      this.model.propNo = obj.propNo
      this.model.propName = obj.propName 
      this.$refs.form.validateField('propName');
    },
  },
}
</script>